package com.tbit.uqbike.webmanager.controller;

import com.tbit.uqbike.object.pojo.RefundLog;
import com.tbit.uqbike.object.pojo.RefundLogSum;
import com.tbit.uqbike.object.pojo.TransactionLog;
import com.tbit.uqbike.object.pojo.dto.RefundLogDTO;
import com.tbit.uqbike.object.pojo.dto.RefundLogDownloadDTO;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.RefundLogService;
import com.tbit.uqbike.webmanager.util.Assert;
import com.tbit.uqbike.webmanager.util.StringUtils;
import com.tbit.uqbike.webmanager.util.csv.annotations.AsyncDownload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
 * 财务统计-退款统计
 *
 * @Author:BUGTian
 * @DATE: 2022/11/16
 */
@RestController
@RequestMapping("/refundLog")
public class RefundLogController {

    @Autowired
    private RefundLogService refundLogService;


    /**
     * 分页查询退款详情
     *
     * @param refundLogDTO
     * @return
     */
    @GetMapping("/queryPage")
    public Result<PageResult<RefundLog>> queryPage(RefundLogDTO refundLogDTO) {
        refundLogDTO.assertParam();
        PageResult<RefundLog> pageResult = refundLogService.queryPage(refundLogDTO);
        return Result.success(pageResult);
    }

    /**
     * 退款统计账单下载
     *
     * @param downloadDTO
     * @return
     */
    @AsyncDownload(defaultFileName = "退款统计账单")
    @GetMapping("/download")
    public Result download(RefundLogDownloadDTO downloadDTO) {
        String url = refundLogService.download(downloadDTO);
        return Result.success(url);
    }

    /**
     * 骑行退款金额分布导出
     *
     * @param downloadDTO .
     * @return .
     */
    @AsyncDownload(defaultFileName = "骑行退款金额分布")
    @GetMapping("/distribution/download")
    public Result<String> distributionDownload(RefundLogDownloadDTO downloadDTO) {
        Date startTime = downloadDTO.getStartTime();
        Date endTime = downloadDTO.getEndTime();
        Integer brandId = downloadDTO.getBrandId();
        Assert.notNulls("请检查是否传入品牌id、开始时间、结束时间", brandId, startTime, endTime);
        String url = refundLogService.distributionDownload(downloadDTO);
        return Result.success(url);
    }

    /**
     * 查询退款汇总
     *
     * @param refundLogDTO
     * @return
     */
    @GetMapping("/sumRefundLog")
    public Result<RefundLogSum> sumRefundLog(RefundLogDTO refundLogDTO) {
        refundLogDTO.assertParamIgnorePage();
        RefundLogSum refundLogSum = refundLogService.sumRefundLog(refundLogDTO);
        return Result.success(refundLogSum);
    }

    /**
     * 查询退款详情
     *
     * @param businessNO 业务订单号
     * @return
     */
    @GetMapping("/selectTransactionLog")
    public Result<List<TransactionLog>> selectTransactionLog(@NotNull String businessNO) {
        if (StringUtils.isEmpty(businessNO)) {
            return Result.error("无数据");
        }
        List<TransactionLog> transactionLogs = refundLogService.selectTransactionLog(businessNO);
        return Result.success(transactionLogs);
    }


}
